<template src="./template.html"></template>
<script>
import { PAGE_CONFIG } from 'common/consts';
import { getCoursePage } from '../models'
import { DEADLINE_STATE_OPTIONS, DEADLINE_STATE } from '../consts'
import dayjs from 'dayjs'

export default {
  data() {
    return {
      DEADLINE_STATE,
      DEADLINE_STATE_OPTIONS,
      isPageLoading: false,
      tableList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      pageSize: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
      filterForm: {
        courseName: '',
        state: ''
      },
    }
  },
  methods: {
    $goCourse(it) {
      this.$router.push({ name: 'curriculum.list.infos', params: { courseId: it.courseId } })
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        limit: this.pageSize,
        page: this.pageNo,
        // state: 0
      };
      try {
        const { data } = await getCoursePage(params);
        this.tableList = (data.list || []).map(item => ({
          ...item,
          deadlineTime: item.isLimitTime === '0' ? '永久' : item.deadlineTime,
          numProcess: item.studentNum ? ((item.hasFinishStuNum / item.studentNum) * 100).toFixed(2) : 0
        }))
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取机构列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList();
    },
    getTimeStr(time) {
      if (time) {
        // 格式化时间
        return dayjs(time).format('YYYY-MM-DD HH:mm')
      } else return '永久'
    }
  },
  created() {
    this.$getTableList();
  },
}
</script>
